import numpy
from flask import Flask, render_template,request,redirect,url_for

import pandas as pd
import numpy as np
from frameworks.utils.JsonResult import *
from frameworks.utils.ApiUtil import *
from frameworks.cores.LoadClass import *
from frameworks.cores.Config import *
from controller.ApiController import *
import json

from flask_socketio import SocketIO
async_mode = None

def create_app():
    app = Flask(__name__)
    return app


app = create_app()

app.config['SECRET_KEY'] = "lyshark"
socketio = SocketIO(app)

basepath = os.path.join(os.path.dirname(__file__))
config = Config(basepath)

apimap = config.getApi()
env = apimap["base"]
print(env)

@app.route(env+"/",methods=['GET'])
def index():
    # print(serverlist)
    apiController = ApiController()
    newParam = {}
    obj = apiController.getHostListData(newParam)
    return render_template('index.html',
                           serverlist = obj
                           )

@app.route(env+"/get_hostlist_data",methods=['GET'])
def getHostlistData():
    # print(serverlist)
    apiController = ApiController()
    newParam = {}
    obj = apiController.getHostListData(newParam)
    return json.dumps(obj)

# 出现消息后,率先执行此处
@socketio.on("message",namespace="/Socket")
def socket(message):
    print("接收到消息:",message['data'])
    for i in range(1,100):
        socketio.sleep(1)
        socketio.emit("response",           # 绑定通信
                      {"Data":i},           # 返回socket数据
                      namespace="/Socket")
# 当websocket连接成功时,自动触发connect默认方法
@socketio.on("connect",namespace="/Socket")
def connect():
    print("链接建立成功..")
# 当websocket连接失败时,自动触发disconnect默认方法
@socketio.on("disconnect",namespace="/Socket")
def disconnect():
    print("链接建立失败..")

if __name__ == "__main__":
    app.run()